package com.pro.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro.schedule.entity.Target;
import com.pro.schedule.filter.ResultUtil;
import com.pro.schedule.mapper.EventMapper;
import com.pro.schedule.mapper.EventTypeChildMapper;
import com.pro.schedule.mapper.TargetMapper;
import com.pro.schedule.service.TargetService;
import com.pro.schedule.util.DatesUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class TargetServiceImpl implements TargetService {
    @Resource
    ResultUtil resultUtil;
    @Resource
    TargetMapper targetMapper;
    @Resource
    EventMapper eventMapper;
    @Resource
    EventTypeChildMapper eventTypeChildMapper;

    @Override
    public IPage<Target> getTargetIPage(String token, Page<Target> page) {
        Long userId = resultUtil.getUserId(token);
        IPage<Target> targetIPage = targetMapper.selectTargetByUserId(page, userId);
        List<Target> targetList = targetIPage.getRecords();
        for (Target target : targetList) {
            Date startDate;
            Date endDate = new Date();
            if (target.getTargetType() == 1) {
                startDate = DatesUtil.getDayBegin();
            } else if (target.getTargetType() == 2) {
                startDate = DatesUtil.getBeginDayOfWeek();
            } else {
                startDate = DatesUtil.getBeginDayOfMonth();
            }
            LocalDateTime startDateTime = DatesUtil.DateToLocalDateTime(startDate);
            LocalDateTime endDateTime = DatesUtil.DateToLocalDateTime(endDate);
            target.setTargetProgress(eventMapper.getEventCount(startDateTime, endDateTime, target.getTargetId()));
        }
        return targetIPage;
    }

    @Override
    public String saveOrUpdateTarget(String token, Target target) throws Exception {
        Long userId = resultUtil.getUserId(token);
        target.setUserId(userId);
        QueryWrapper<Target> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("target_detail", target.getTargetDetail());
        if (target.getTargetId() == null) {
            int count = targetMapper.selectCount(wrapper);
            if (count > 0) {
                return "已存在相应目标";
            }
            target.setTargetStatus(true);
            int result = targetMapper.insert(target);
            if (result > 0) {
                eventMapper.updateEventByTargetId(target.getTargetId(), userId, target.getTargetDetail());
            }
            return resultUtil.getResult(result, "保存");
        } else {
            Target oldTarget = targetMapper.selectById(target.getTargetId());
            if (!Objects.equals(oldTarget.getTargetDetail(), target.getTargetDetail())) {
                int count = targetMapper.selectCount(wrapper);
                if (count > 0) {
                    return "已存在相应目标";
                }
            }
            target.setTargetStatus(oldTarget.getTargetStatus());
            int result = targetMapper.updateById(target);
            return resultUtil.getResult(result, "修改");
        }
    }

    @Override
    public String deleteTarget(Integer targetId) throws Exception {
        int result = targetMapper.deleteById(targetId);
        if (result > 0) {
            eventMapper.clearEventByTargetId(targetId);
            eventTypeChildMapper.clearEventTypeChildByTargetId(targetId);
        }
        return resultUtil.getResult(result, "删除");
    }

    @Override
    public String changeTargetStatus(Integer targetId) throws Exception {
        Target target = targetMapper.selectById(targetId);
        target.setTargetStatus(!target.getTargetStatus());
        int result = targetMapper.updateById(target);
        return resultUtil.getResult(result, "修改状态");
    }

    @Override
    public int getTargetCount(String token) {
        Long userId = resultUtil.getUserId(token);
        QueryWrapper<Target> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return targetMapper.selectCount(wrapper);
    }
}
